package com.yixing.tech.wms.statistics;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BaseResponse;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.wms.statistics.service.IInventoryDataService;
import com.yixing.tech.wms.stock.out.StockOutService;
import com.yixing.tech.wms.stock.out.domain.Inventory;
import com.yixing.tech.wms.stock.out.domain.StockOutOperation;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/wms/tbea/statistics")
@Slf4j
public class StatisticsController extends BaseControl {

    @Autowired
    StockOutService stockOutService;

//    @Autowired
//    StkInventoryMapper stkInventoryMapper;

    @Autowired
    IInventoryDataService inventoryDataService;

    @GetMapping("/inventory")
    public Rs<Object> inventory() throws BusinessException {

        List<Inventory> list = stockOutService.queryInventoryWithoutStatistics();
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc(Integer.toString(list.size()));
        response.setData(list);
        return response;
    }

    @GetMapping("/inventoryOperationFlow")
    public Rs<Object> inventoryOperationFlow() throws BusinessException {

        List<StockOutOperation> list = stockOutService.queryStockOutOperationWithoutStatistics();
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc(Integer.toString(list.size()));
        response.setData(list);
        return response;
    }

//    @GetMapping("/selectInventoryAll")
//    public Rs<Object> selectInventoryAll() throws BusinessException {
//
//        QueryWrapper<StkInventory> wrapper = new QueryWrapper<StkInventory>().gt("real_num", 0);
//
//        List<StkInventory> list = stkInventoryMapper.selectList(wrapper);
//        BaseResponse<Object> response = new BaseResponse<>();
//        response.setCode(1000);
//        response.setDesc(Integer.toString(list.size()));
//        response.setData(list);
//        return response;
//    }

    @PostMapping("/insertMesTable")
    public Rs<Object> insertMesTable(@RequestBody Inventory inventory) throws BusinessException {

        inventoryDataService.insertInventoryIntoMesTable(inventory);
        return Rs.ok("插入成功");
    }

    @PostMapping("/updateMesTable")
    public Rs<Object> updateMesTable(@RequestBody Inventory inventory) throws BusinessException {

        inventoryDataService.updateInventoryMesTable(inventory);
        return Rs.ok("更新成功");
    }

    @PostMapping("/selectInventoryMesTable")
    public Rs<Object> selectInventoryMesTable() throws BusinessException {

        List<Document> list = stockOutService.queryInventoryIdsRealNumGtZero();
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc(Integer.toString(list.size()));
        response.setData(list);
        return response;
    }

    @PostMapping("/initMesInventoryTable")
    public Rs<Object> insertIntoRedis() throws BusinessException {

        inventoryDataService.syncInventoryIntoMesTable();
        return Rs.ok("response");
    }
}
